﻿using System;
using System.Web.Mvc;
using MvcForum.Net.Business;
using MvcForum.Net.Enums;
using MvcForum.Net.Utils;
using Nasa8x.Core;
using Nasa8x.Mvc.Helpers.Attributes;
using MainDB = MvcForum.Net.Business.MainDB;

namespace MvcForum.Net.Controllers
{
    public class CPanelController : Controller
    {
        //
        // GET: /ControlPanel/
        [RequiresAuthentication]
        public ActionResult Index()
        {

            var _page = new PageEntity();
            _page.Name = "Bảng điều khiển";
            _page.Url = ForumContext.GetAbsoluteUrl(ForumUrls.CONTROL_PANEL);

            ViewData["Page"] = _page;

            return View();
        }
        [RequiresAuthentication]
        public ActionResult ChangePassword()
        {
            return View();
        }


        [RequiresAuthentication]
        public ActionResult Home()
        {

            var _u = UsersRepository.UserInfo(ForumContext.UserInfo.UserId);
            return View(_u);
        }
        [RequiresAuthentication]
        public ActionResult Avatar()
        {

            var _u = UsersRepository.UserInfo(ForumContext.UserInfo.UserId);
            return View(_u);
        }

        [RequiresAuthentication]
        public ActionResult ContactDetails()
        {

            var _u = UsersRepository.UserInfo(ForumContext.UserInfo.UserId);
            return View(_u);
        }
        [RequiresAuthentication]
        public ActionResult EmailSignature()
        {

            var _u = UsersRepository.UserInfo(ForumContext.UserInfo.UserId);
            return View(_u);
        }




        [HttpPost]
        [ValidateAntiForgeryToken(Salt = "CHANGE_PWD")]
        [RequiresAuthentication]
        public ActionResult UpdatePassword(FormCollection form)
        {

            bool _success = false;
            string _msg = "Nhập sai mật khẩu cũ";

            string _oldPwd = Encryptor.Password(ForumContext.UserInfo.UserName, form["oldpwd"]);
            string _newPwd = Encryptor.Password(ForumContext.UserInfo.UserName, form["newpwd"]);

            if (UsersRepository.ChangePwd(ForumContext.UserInfo.UserId, _oldPwd, _newPwd))
            {
                _success = true;
                _msg = "Đổi mật khẩu thành công";

            }

            return Json(new
                            {

                                success = _success,
                                message = _msg
                            });
        }


        [HttpPost]
        [ValidateAntiForgeryToken(Salt = "UPDATE_CONTACT")]
        [RequiresAuthentication]
        public ActionResult UpdateContact(FormCollection form)
        {


            var _db = new MainDB();
            _db.UsersSaveContactDetails(ForumContext.UserInfo.UserId, form["fullname"], form["address"], 3, DateTime.Parse(form["birthday"]), form["website"], form["blog"], form["ym"], form["sk"], form["msn"], form["aol"]).Execute();

            bool _success = true;
            string _msg = "Cập nhật thành công";


            return Json(new
                            {

                                success = _success,
                                message = _msg
                            });
        }


        [HttpPost]
        [ValidateAntiForgeryToken(Salt = "UPDATE_AVATAR")]
        [RequiresAuthentication]
        public ActionResult UpdateAvatar(FormCollection form)
        {


            string _avatar = form["RemoteUrl"];
            string _msg = "Cập nhật thành công";


            if (Nasa8x.Core.Utils.IsGraphic(_avatar))
            {

                if (Nasa8x.Core.Utils.RemoteUrlSize(_avatar) < ForumContext.Configuration.AvatarSize)
                {
                    var _db = new MainDB();
                    _db.Update<User>().Set(x => x.Avatar == _avatar).Where(x => x.UserId == ForumContext.UserInfo.UserId).Execute();
                }
                else
                {

                    _msg = ForumContext.GetMessage(Messages.PostAttachmentTooLarge);
                }

            }

            else
            {

                _msg = ForumContext.GetMessage(Messages.FileTypeInvalid);

            }


            return Json(new
            {

                success = true,
                message = _msg
            });
        }



        [HttpPost]
        [ValidateAntiForgeryToken(Salt = "EMAIL_SIGNATURE")]
        [ValidateInput(false)]
        [RequiresAuthentication]
        public ActionResult UpdateEmailSignature(FormCollection form)
        {
            bool _allow;
            bool _success = true;
            string _msg = "Cập nhật thành công";
            var _email = form["email"];
            if (_email.ToLower().Trim() == ForumContext.UserInfo.Email.ToLower())
            {
                _allow = true;

            }
            else
            {
                _allow = !UsersRepository.EmailExists(_email);
            }

            if (_allow)
            {
                string _sign = form["signature"].HtmlSanitize();

                //if(string.IsNullOrEmpty(_sign.Trim()))
                //{
                //    _sign = string.Empty;
                //}
                new MainDB().UsersSaveEmailSignature(ForumContext.UserInfo.UserId, _email, _sign).Execute();


            }
            else
            {
                _success = false;
                _msg = "Địa chỉ mail đã tồn tại";
            }


            return Json(new
            {

                success = _success,
                message = _msg
            });
        }





    }
}
